﻿Imports EVOLTA.Promocion.Bl.Bc
Imports EVOLTA.Promocion.Bl.Be

Partial Class mantenedores_instanciasTP
    Inherits System.Web.UI.Page

    Private instanciaTP As List(Of EinstanciasTP) = Nothing
    Dim expression As String = Nothing
    Dim direction As SortDirection = SortDirection.Ascending

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.AddHeader("Pragma", "no-cache")
        Response.Expires = -1
        '***********************************************************'
        'btnAgregar.Attributes.Add("onclick", "MuestraPanel(); return false;")
        If Not IsPostBack Then
            cargarInstanciasTP()
            cargarEstado()
        End If
        Dim scriptText As String = ""
        scriptText &= "function mensaje(){"
        scriptText &= "    alert('se graba correctamente.');"
        scriptText &= "}"
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "mensaje()", scriptText, True)
    End Sub

    Private Function Buscar(ByVal filtro As String) As IEnumerable(Of EinstanciasTP)
        Dim LInstanciaTP As System.Collections.Generic.IEnumerable(Of EinstanciasTP) = Nothing
        instanciaTP = Session("lista")
        If Not instanciaTP Is Nothing Then
            If filtro.Length > 0 Then
                Dim x As Int32 = Asc(filtro.Substring(0, 1).Trim)
                If x >= 48 AndAlso x <= 57 Then
                    filtro = CType(filtro, Int32)
                    LInstanciaTP = From v In instanciaTP _
                                Where v.idInstanciaTP = filtro _
                                Select v
                    Return LInstanciaTP
                Else
                    LInstanciaTP = From v In instanciaTP _
                                Where v.descInstanciaTP Like "*" + filtro + "*" _
                                Select v
                End If
            Else
                LInstanciaTP = From v In instanciaTP _
                            Where v.descInstanciaTP Like "*" + filtro + "*" _
                            Select v
            End If
        End If
        Return LInstanciaTP
    End Function

    Private Sub cargarInstanciasTP()
        instanciaTP = RinstanciaTP.SeleccionarTodo(Me.Master.descEmpresa, False)
        Session("lista") = instanciaTP
        If Not instanciaTP Is Nothing Then
            gvwLista.DataSource = instanciaTP
            gvwLista.DataBind()
        End If
    End Sub

    Protected Sub lnkBtn_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) _
    Handles lnkBtnCodigo.Command, lnkBtnNombre.Command, lnkBtnEstado.Command
        Dim tinstanciaTP As IEnumerable(Of EinstanciasTP) = Buscar("")

        Dim List = From d In tinstanciaTP _
                   Order By d.descInstanciaTP _
                   Select d
        gvwLista.DataSource = List
        gvwLista.DataBind()
    End Sub
    Private Sub cargarEstado()
        Dim Eestado As New Ecommons
        selEstado.DataSource = Eestado.estado
        selEstado.DataTextField = "descripcion"
        selEstado.DataValueField = "codigo"
        selEstado.DataBind()
    End Sub
    Protected Sub gvwLista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwLista.PageIndexChanging

    End Sub
    Protected Sub gvwLista_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvwLista.RowCommand
        Select Case e.CommandName
            Case "Delete"
                Dim instanciaTP As New EinstanciasTP
                instanciaTP.idInstanciaTP = e.CommandArgument
                instanciaTP.idUsuario = Me.Master.idUsuario
                RinstanciaTP.Eliminar(Me.Master.descEmpresa, instanciaTP)
                cargarInstanciasTP()
        End Select
    End Sub
    Protected Sub gvwLista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwLista.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gvwLista, "Select$" + e.Row.RowIndex.ToString)
            e.Row.Style.Add("cursor", "hand")
        End If
    End Sub
    Protected Sub gvwLista_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwLista.RowDeleting

    End Sub
    Protected Sub gvwLista_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvwLista.RowEditing
        Dim id As Int32 = CType(CType(gvwLista.Rows(e.NewEditIndex).FindControl("lnkCodigo"), LinkButton).Text, Int32)
        instanciaTP = Session("lista")
        Dim Linstancia = From v In instanciaTP _
                            Where v.idInstanciaTP.ToString = id _
                            Select v
        lblTitulo.Text = "INSTANCIAS [Modificar]"
        lblTitulo.ToolTip = "Modificar"
        txtId.Text = Linstancia.ElementAt(0).idInstanciaTP
        txtInstanciaTP.Value = Linstancia.ElementAt(0).descInstanciaTP
        txtFechaInstanciaTP.Value = Linstancia.ElementAt(0).fechaCreacion
        txtNombreUsuario.Value = Linstancia.ElementAt(0).descUsuario
        selEstado.SelectedIndex = Linstancia.ElementAt(0).estado
        mpDetalle.Show()
        'lblTitulo.ToolTip = "Registrar"
    End Sub
    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        Dim pinstanciaTP As New EinstanciasTP
        If txtInstanciaTP.Value.Trim.Length > 0 Then
            Dim jsMensaje As String = Nothing
            If lblTitulo.ToolTip = "Registrar" Then
                pinstanciaTP = New EinstanciasTP
                With pinstanciaTP
                    .descInstanciaTP = txtInstanciaTP.Value.Trim.ToUpper
                    .idUsuario = Me.Master.idUsuario
                End With
                Dim resultado As Boolean = _
                        RinstanciaTP.Añadir(Me.Master.descEmpresa, pinstanciaTP)
                If resultado Then
                    jsMensaje = "<script>javascript:alert(' Se registro correctamente.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                    cargarInstanciasTP()
                Else
                    jsMensaje = "<script>javascript:alert(' No se pudo registrar.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                End If
            ElseIf lblTitulo.ToolTip = "Modificar" Then
                pinstanciaTP = New EinstanciasTP
                With pinstanciaTP
                    .idInstanciaTP = CType(txtId.Text, Int32)
                    .descInstanciaTP = txtInstanciaTP.Value.Trim.ToUpper
                    .estado = Convert.ToString(selEstado.SelectedIndex)
                    .idUsuario = Me.Master.idUsuario
                End With
                RinstanciaTP.Editar(Me.Master.descEmpresa, pinstanciaTP)
                jsMensaje = "<script>javascript:alert(' Se modifico correctamente.');</script>)"
                Controls.Add(New LiteralControl(jsMensaje))
                cargarInstanciasTP()
            End If
            lblTitulo.ToolTip = "Registrar"
        End If
    End Sub
    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        lblTitulo.ToolTip = "Registrar"
        mpDetalle.CancelControlID = "btnCancelar"
    End Sub

    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        lblTitulo.Text = "INSTANCIAS [Registrar]"
        lblTitulo.ToolTip = "Registrar"
        txtId.Text = 0
        txtInstanciaTP.Value = ""
        txtFechaInstanciaTP.Value = DateTime.Now.ToShortDateString
        txtFechaInstanciaTP.Disabled = True
        txtNombreUsuario.Value = Master.descUsuario
        txtNombreUsuario.Disabled = True
        selEstado.SelectedIndex = 1
        mpDetalle.Show()
    End Sub
End Class
